package com.cooldatasoft.domain.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Type;

@SuppressWarnings("serial")
@Entity
@Table(name = "DocumentEntry", catalog = "ws1259000db")
public class DocumentEntry implements Serializable, Comparable<DocumentEntry> {
	
	private long id;
	private int orderInGroup;
	private String header;
	private String text;
	private Document document;
	private List<DocumentEntry> documentEntries = new ArrayList<DocumentEntry>();
	private DocumentEntry parentEntry;
	
	public DocumentEntry() {}
	
	public DocumentEntry(long id_in, String header_in, String text_in, Document doc_in, List<DocumentEntry> entries_in) {
		this.id = id_in;
		this.header = header_in;
		this.text = text_in;
		this.document = doc_in;
		this.documentEntries = entries_in;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id_documentEntry", unique = true, nullable = false)
	public long getId() {
		return this.id;
	}
	
	public void setId(long id_in) {
		this.id = id_in;
	}
	
	@Column(name = "orderInGroup", nullable = false)
	public int getOrderInGroup() {
		return this.orderInGroup;
	}

	public void setOrderInGroup(int order_in) {
		this.orderInGroup = order_in;
	}
	
	@Column(name = "header", nullable = false, length = 250)
	public String getHeader() {
		return this.header;
	}

	public void setHeader(String header_in) {
		this.header = header_in;
	}
	
	@ManyToOne
	public DocumentEntry getParentEntry() {
		return this.parentEntry;
	}

	public void setParentEntry(DocumentEntry entry_in) {
		this.parentEntry = entry_in;
	}
	
	@Column(name = "text", nullable = false)
	@Type(type = "text")
	public String getText() {
		return this.text;
	}

	public void setText(String text_in) {
		this.text = text_in;
	}
	
	@ManyToOne
	@JoinColumn(name="id_document",nullable = false)
	public Document getDocument() {
		return this.document;
	}

	public void setDocument(Document doc_in) {
		this.document = doc_in;
	}
	
	@OneToMany(fetch = FetchType.EAGER, mappedBy = "parentEntry")
	public List<DocumentEntry> getDocumentEntries() {
		return this.documentEntries;
	}

	public void setDocumentEntries(List<DocumentEntry> entries_in) {
		this.documentEntries = entries_in;
	}
	
	@Override
	public String toString() {
		return getIndexForDocumentation() + " " + header;
	}

	@Override
	public int compareTo(DocumentEntry entry_in) {
		 return this.orderInGroup < entry_in.orderInGroup ? -1 : 1;
	}
	
	@Transient
	public String getIndexForDocumentation() {
		String result = determineIndex();
		if (result.length() == 0) {
			return "";
		}
		return result.substring(0, result.length() - 1);
	}
	
	@Transient
	public String determineIndex() {
		StringBuilder builder = new StringBuilder();
		
		// 0 is top section, it won't be showns 
		if (orderInGroup != 0) {
			
			builder.append(String.valueOf(orderInGroup) + ".");
			
			DocumentEntry entry = getParentEntry();
			if (entry != null) {
				builder.insert(0, entry.determineIndex());
			}
		}
		return builder.toString();
	}
}
